package santeclair.referentiel.gestionnaire.webservice.rest.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import santeclair.referentiel.bean.Organisme;
import santeclair.referentiel.gestionnaire.bean.BureauGestion;
import santeclair.referentiel.gestionnaire.bean.TypeContrat;
import santeclair.referentiel.gestionnaire.dto.BureauGestionDto;
import santeclair.referentiel.gestionnaire.service.BureauGestionService;
import santeclair.referentiel.gestionnaire.service.TypeContratService;
import santeclair.referentiel.gestionnaire.webservice.BureauGestionWebService;
import santeclair.referentiel.service.OrganismeService;

@Service("bureauGestionWebService")
public class BureauGestionWebServiceImpl implements BureauGestionWebService {
    /** Service de consultation des organismes. */
    private OrganismeService organismeService;
    /** Service de consultation des type de contrat. */
    private TypeContratService typeContratService;
    /** service de consultation des bureau de gestion. */
    private BureauGestionService bureauGestionService;

    /**
     * 
     */
    public BureauGestionWebServiceImpl() {
    }

    /**
     * Constructeur.
     * 
     * @param organismeService
     * @param typeContratService
     * @param bureauGestionService
     */
    @Autowired
    public BureauGestionWebServiceImpl(OrganismeService organismeService, TypeContratService typeContratService, BureauGestionService bureauGestionService) {
        this.organismeService = organismeService;
        this.typeContratService = typeContratService;
        this.bureauGestionService = bureauGestionService;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public BureauGestionDto getBureauGestion(String codeOrganisme, String codeTypeContrat, String codeBRG) {
        Organisme organisme = organismeService.getByCode(codeOrganisme);
        TypeContrat typeContrat = typeContratService.findTypeContratByCode(codeTypeContrat);

        BureauGestion bureauGestion = bureauGestionService.getBureauGestion(organisme, typeContrat, codeBRG);

        if (bureauGestion != null) {
            BureauGestionDto result = new BureauGestionDto();
            result.setCode(bureauGestion.getCode());
            result.setCodeSIA(bureauGestion.getCodeSIA());
            result.setEmailDemandesGarantie(bureauGestion.getEmailDemandesGarantie());
            result.setLibelle(bureauGestion.getLibelle());
            result.setOrganisme(bureauGestion.getOrganisme().getCode());
            result.setLibelleOrganisme(bureauGestion.getOrganisme().getLibelle());
            result.setTypeContrat(codeTypeContrat);

            return result;
        }
        return null;
    }

}
